// app.js
import api from './utils/api'
import dialog from './utils/dialog'
import apiInstance from "./utils/apiInstance"
import awbApiInstance from "./utils/awbApiInstance"
import {getPassword, getSchoolid, getUserName, setToken, setNickName, setSysId, getUserInfo} from './utils/store';
import moment from './public/utils/moment'
import env from "./utils/env"
import store from "./store/index";



App({
    api: api,
    dialog: dialog,
    apiInstance,
    awbApiInstance,
    moment,
    store,
    globalData: {
        system: '',
        capsule: '',
        signUrl: env.signUrl,
        baseDomain: env.baseDomain,
        userInfo: {},
        windowHeight: 0,
        awbUrl: env.awbUrl,
        baseUrl: env.baseUrl,
        fileUrl: env.fileUrl,
        towerUrl: env.towerUrl,
        navParam: {
            name: "测试",
            spec: "33333333",
            position: ""
        },
        yinHuanGzData: {}, //隐患跟踪
    },
    /**
     * 启动参数
     * @param obj
     */
    onLaunch(obj) {
        console.log('onLauch参数：' + JSON.stringify(obj))
        let that = this;
        this.checkUpdateVersion();

        // obj={"path":"pages/awb/pound/index","query":{},"scene":1001,"referrerInfo":{},"mode":"default","apiCategory":"default"}
        // awb相关的不需要 sap信息。
        if (obj && obj.path && obj.path.indexOf('awb') != -1) {
            // 不需要其他操作
        } else {
            this.checkAuth();
        }
        //获取缓存中的数据
        wx.getSystemInfo({
            success: e => {
                this.globalData.StatusBar = e.statusBarHeight;
                this.globalData.system = e;
                let capsule = wx.getMenuButtonBoundingClientRect();
                this.globalData.capsule = capsule
                if (capsule) {
                    this.globalData.Custom = capsule;
                    this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
                } else {
                    wx.showModal({
                        title: '提示',
                        content: JSON.stringify(capsule),
                        success(res) {
                        }
                    })
                    this.globalData.CustomBar = e.statusBarHeight + 50;
                }
                this.globalData.windowHeight = e.windowHeight - e.statusBarHeight - this.globalData.CustomBar;//获取屏幕高度
            },
            fail: e => {
                console.log(e);
            }
        })

    },

    onBackPress() {

    },


    //检测登录状态
    checkAuth() {
        const userinfo = getUserInfo('userinfo')

        //如果有sapid 直接返回首页
        if (!userinfo.staffId) {
            wx.reLaunch({
                url: '/pages/login/index',
            })
        }
        else {
            store.setState({
                userInfo: userinfo
            })
        }
    },


    doLogin() {
        try {
            wx.login({
                success: res => {
                    api.login({
                        code: res.code,
                        schoolid: getSchoolid(),
                    }).then(res => {
                        if (res.code == 200) {
                            setToken('Bearer ' + res.data.token)
                            api.getUserInfo().then(res1 => {
                                setNickName(res1.data.user.nickName)
                                setSysId(res1.data.user.sysId)
                                if (res1.code == 200) {
                                    wx.reLaunch({
                                        url: '/pages/home/home',
                                    })
                                }
                            })
                        } else {
                            wx.navigateTo({
                                url: '/pages/login/login',
                            })
                        }
                    })
                }
            })
        } catch (e) {
            console.log(e);
        }
    },
    /**
     * 检测当前的小程序
     * 是否是最新版本，是否需要下载、更新
     */
    checkUpdateVersion() {
        //创建 UpdateManager 实例
        const updateManager = wx.getUpdateManager();
        //检测版本更新
        updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            if (res.hasUpdate) {
                //监听小程序有版本更新事件
                updateManager.onUpdateReady(function () {
                    wx.showModal({
                        title: '更新提示',
                        content: '新版本已经准备好，是否重启应用？',
                        success(res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate();
                            }
                        }
                    })
                })

                updateManager.onUpdateFailed(function () {
                    // 新版本下载失败
                    wx.showModal({
                        title: '已经有新版本咯~',
                        content: '请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开呦~',
                    })
                })
            }
        })
    }
})
